package com.leha.mp.core.services.imp;

import com.leha.mp.core.dao.MpUserMapper;
import com.leha.mp.core.dao.PlDockerUserRelationMapper;
import com.leha.mp.core.entity.mybatis.MpUser;
import com.leha.mp.core.entity.mybatis.MpUserExample;
import com.leha.mp.core.entity.mybatis.PlDockerUserRelation;
import com.leha.mp.core.entity.mybatis.PlDockerUserRelationKey;
import com.leha.mp.core.services.MpUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MpUserServicesImpl implements MpUserServices {
    @Autowired
    MpUserMapper mpUserMapper;
    @Autowired
    PlDockerUserRelationMapper dockerUserRelationMapper;

    @Override
    public MpUser saveUser(MpUser u) {
        MpUserExample ex = new MpUserExample();
        ex.createCriteria().andOpenidEqualTo(u.getOpenid());
        List<MpUser> res = mpUserMapper.selectByExample(ex);
        if(res.size()>0) {
            return res.get(0);
        }
        mpUserMapper.insertSelective(u);
        return u;
    }

    @Override
    public Boolean initRelation(String unionId, Integer doctorId) {
        PlDockerUserRelation key = new PlDockerUserRelation();
        key.setDockerId(doctorId);
        key.setUnionid(unionId);
        try{
            return dockerUserRelationMapper.insertSelective(key)>0;
        } catch (Exception e) {
            return false;
        }
    }
}
